define(['./../../initial'],
    function() {
        var app = angular.module('App.home');
        app.controller('admin.user.configCtrl', ['$scope', '$rootScope', '$state', '$stateParams', 'User', 'showMsg', 'Project', 'authorizedUsers', 'curPerson', 'cityList',
            function($scope, $rootScope, $state, $stateParams, User, showMsg, Project, authorizedUsers, curPerson, cityList) {
                $scope.curPerson = curPerson;
                //获取工程列表
                $scope.cityList = cityList;

                //是否可以编辑用户的权限项目
                $scope.canEdit = function() {
                    return !curPerson.hasAdminPower();
                };

                function parseAuthorizedProjects() {
                    $scope.cityList.forEach(function(city) {
                        city.lines.forEach(function(line) {
                            line.projects.forEach(function(prj) {
                                if ($scope.curPerson.authorizedProjectList.indexOf(prj.prjId) >= 0) {
                                    prj.checked = true;
                                } else {
                                    prj.checked = false;
                                }
                            });
                        });
                    });
                }
                parseAuthorizedProjects();


                $scope.togglePrjCheck = function(prj) {
                    if (!$scope.canEdit) return;
                    prj.checked = !prj.checked;
                };

                $scope.updateAuthorizedProjectList = function() {
                    var list = [];
                    $scope.cityList.forEach(function(city) {
                        city.lines.forEach(function(line) {
                            line.projects.forEach(function(prj) {
                                if (prj.checked === true) {
                                    list.push(prj.prjId);
                                }
                            });
                        });
                    });
                    $scope.curPerson.authorizedProjectList = list;
                    $scope.curPerson.postAuthorizedProjectList().then(function(data) {
                        if (data.toString() === 'true') {
                            showMsg('已成功设置权限项目', 'info');
                        } else {
                            showMsg('出现错误', 'alert');
                        }
                    });
                };

            }
        ]);
    });
